import { mockRequestClient as requestClient } from '#/api/request';

enum Api {
  GetMonitorData = '/resource-center/saas/applications/monitor',
  GetApplicationLogs = '/resource-center/saas/applications/logs',
  GetAlerts = '/resource-center/saas/applications/alerts',
  SaveAlert = '/resource-center/saas/applications/alerts',
  DeleteAlert = '/resource-center/saas/applications/alerts',
  GetHealthStatus = '/resource-center/saas/applications/health',
  GetResponseTimes = '/resource-center/saas/applications/response-times',
}

export interface MonitorData {
  cpu: number;
  memory: number;
  disk: number;
  network: number;
  timestamp: string;
}

export interface ApplicationLog {
  id: number;
  timestamp: string;
  level: 'info' | 'warn' | 'error' | 'debug';
  message: string;
  source: string;
}

export interface AlertRule {
  id: number;
  name: string;
  metric: string;
  threshold: number;
  duration: number;
  enabled: boolean;
  notifications: string[];
  description?: string;
}

export interface HealthService {
  name: string;
  status: 'healthy' | 'unhealthy' | 'warning';
  description: string;
  lastCheck: string;
}

export interface ResponseTime {
  path: string;
  time: number;
  status: number;
  timestamp: string;
}

// 获取监控数据
export const getMonitorData = (applicationId: string) => {
  return requestClient.get(`${Api.GetMonitorData}/${applicationId}`);
};

// 获取应用日志
export const getApplicationLogs = (applicationId: string, params?: any) => {
  return requestClient.get(`${Api.GetApplicationLogs}/${applicationId}`, {
    params,
  });
};

// 获取告警规则
export const getAlerts = (applicationId: string) => {
  return requestClient.get(`${Api.GetAlerts}/${applicationId}`);
};

// 保存告警规则
export const saveAlert = (applicationId: string, alert: AlertRule) => {
  if (alert.id) {
    return requestClient.put(
      `${Api.SaveAlert}/${applicationId}/${alert.id}`,
      alert,
    );
  } else {
    return requestClient.post(`${Api.SaveAlert}/${applicationId}`, alert);
  }
};

// 删除告警规则
export const deleteAlert = (applicationId: string, alertId: number) => {
  return requestClient.delete(`${Api.DeleteAlert}/${applicationId}/${alertId}`);
};

// 获取健康状态
export const getHealthStatus = (applicationId: string) => {
  return requestClient.get(`${Api.GetHealthStatus}/${applicationId}`);
};

// 获取响应时间
export const getResponseTimes = (applicationId: string) => {
  return requestClient.get(`${Api.GetResponseTimes}/${applicationId}`);
};
